'''
Created on Feb 22, 2012

@author: daoxuandung
'''

"""
Provide URL routing for the whole website
"""

import webapp2
from google.appengine.ext import ndb
from webapp2_extras.routes import RedirectRoute

routes = [
          # Home page
          RedirectRoute('/home/', 
                      handler='web.home_page.HomePageHandler', 
                      name = 'home-page',
                      strict_slash=True),
          # User page
          RedirectRoute('/user/<author_id:\d+>/', 
                      handler='web.user_page.UserPageHandler', 
                      name = 'user-page',
                      strict_slash=True),
          # Board page
          RedirectRoute('/boards/',
                      handler='web.board_page.BoardPageHandler', 
                      name = 'board-page',
                      strict_slash=True),
       
          # Article
          RedirectRoute('/api/article/create/', 
                      handler='api.article.CreateHandler', 
                      name = 'article-create',
                      strict_slash=True),
          RedirectRoute('/api/article/read/<bill_id:\d+>/', 
                      handler='api.article.ReadHandler', 
                      name = 'article-read',
                      strict_slash=True),
          RedirectRoute('/api/article/update/', 
                      handler='api.article.UpdateHandler', 
                      name = 'article-update',
                      strict_slash=True),
          RedirectRoute('/api/article/delete/', 
                      handler='api.article.DeleteHandler', 
                      name = 'article-delete',
                      strict_slash=True),
          RedirectRoute('/api/article/', 
                      handler='api.article.ArticlesHandler', 
                      name = 'article-all',
                      strict_slash=True),     
          RedirectRoute('/api/article/top-view/', 
                      handler='api.article.TopViewArticlesHandler', 
                      name = 'article-top-view',
                      strict_slash=True),     
          RedirectRoute('/api/article/top-vote/', 
                      handler='api.article.TopVoteArticlesHandler', 
                      name = 'article-top-vote',
                      strict_slash=True),     
          RedirectRoute('/api/article/cursor/', 
                      handler='api.article.CursorHandler', 
                      name = 'article-cursor',
                      strict_slash=True),     

          RedirectRoute('/api/article/increase-view/', 
                      handler='api.article.IncreaseViewHandler', 
                      name = 'article-increase-view',
                      strict_slash=True),     
               
          # Vote
          RedirectRoute('/api/vote/create/', 
                      handler='api.vote.CreateHandler', 
                      name = 'vote-create',
                      strict_slash=True),
          RedirectRoute('/api/vote/read/<bill_id:\d+>/', 
                      handler='api.vote.ReadHandler', 
                      name = 'vote-read',
                      strict_slash=True),
          RedirectRoute('/api/vote/update/', 
                      handler='api.vote.UpdateHandler', 
                      name = 'vote-update',
                      strict_slash=True),
          RedirectRoute('/api/vote/delete/', 
                      handler='api.vote.DeleteHandler', 
                      name = 'vote-delete',
                      strict_slash=True),

          # Comment
          RedirectRoute('/api/comment/create/', 
                      handler='api.comment.CreateHandler', 
                      name = 'comment-create',
                      strict_slash=True),
          RedirectRoute('/api/comment/read/<bill_id:\d+>/', 
                      handler='api.comment.ReadHandler', 
                      name = 'comment-read',
                      strict_slash=True),
          RedirectRoute('/api/comment/update/', 
                      handler='api.comment.UpdateHandler', 
                      name = 'comment-update',
                      strict_slash=True),
          RedirectRoute('/api/comment/delete/', 
                      handler='api.comment.DeleteHandler', 
                      name = 'comment-delete',
                      strict_slash=True),
          RedirectRoute('/api/comment/url/', 
                      handler='api.comment.CommentUrlHandler', 
                      name = 'comment-url',
                      strict_slash=True),
          
          # User
          RedirectRoute('/api/user/create/', 
                      handler='api.user.CreateHandler', 
                      name = 'user-create',
                      strict_slash=True),
          RedirectRoute('/api/user/read/<bill_id:\d+>/', 
                      handler='api.user.ReadHandler', 
                      name = 'user-read',
                      strict_slash=True),
          RedirectRoute('/api/user/update/', 
                      handler='api.user.UpdateHandler', 
                      name = 'user-update',
                      strict_slash=True),
          RedirectRoute('/api/user/delete/', 
                      handler='api.user.DeleteHandler', 
                      name = 'user-delete',
                      strict_slash=True),
          RedirectRoute('/api/user/follow/', 
                      handler='api.user.FollowUserHandler', 
                      name = 'user-follow',
                      strict_slash=True),

          # Scraper
          RedirectRoute('/api/scraper/get-photos/', 
                      handler='api.scraper.GetPhotosHandler', 
                      name = 'scraper-get-photos',
                      strict_slash=True),
          
          # Board list
          RedirectRoute('/api/board/create/', 
                      handler='api.board.CreateHandler', 
                      name = 'board-create',
                      strict_slash=True),
          RedirectRoute('/api/board/read/<board_id:\d+>/', 
                      handler='api.board.ReadHandler', 
                      name = 'board-read',
                      strict_slash=True),
          RedirectRoute('/api/board/update/', 
                      handler='api.board.UpdateHandler', 
                      name = 'board-update',
                      strict_slash=True),
          RedirectRoute('/api/board/delete/', 
                      handler='api.board.DeleteHandler', 
                      name = 'board-delete',
                      strict_slash=True),
          RedirectRoute('/api/board/add-note/', 
                      handler='api.board.AddNoteToBoard', 
                      name = 'board-add-note',
                      strict_slash=True),
          RedirectRoute('/api/board/<board_id:\d+>/notes/', 
                      handler='api.board.NotesInBoardHandler', 
                      name = 'board-notes',
                      strict_slash=True),
          RedirectRoute('/api/board/follow/', 
                      handler='api.board.FollowBoardHandler', 
                      name = 'board-follow',
                      strict_slash=True),
          RedirectRoute('/api/board/cursor/', 
                      handler='api.board.CursorHandler', 
                      name = 'board-cursor',
                      strict_slash=True),


          # Cron
          RedirectRoute('/api/cron/update-stat/', 
                      handler='api.cron.UpdateStatHandler', 
                      name = 'update-stat',
                      strict_slash=True),
          RedirectRoute('/api/cron/user-setup/', 
                      handler='api.cron.UserSetupHandler', 
                      name = 'cron-user-setup',
                      strict_slash=True),
          # Auth
          RedirectRoute('/auth/login/', 
                      handler='web.authenticate.LoginHandler', 
                      name = 'login',
                      strict_slash=True),
          RedirectRoute('/auth/openid/<provider>/', 
                      handler='web.authenticate.OpenIDHandler', 
                      name = 'openid'),
          RedirectRoute('/auth/openid-result/<provider>/', 
                      handler='web.authenticate.OpenIDResultHandler', 
                      name = 'openid-result'),
          RedirectRoute('/auth/logout/', 
                      handler='web.authenticate.LogoutHandler', 
                      name = 'logout',
                      strict_slash=True),
          RedirectRoute('/auth/register/', 
                      handler='web.authenticate.RegisterHandler', 
                      name = 'register',
                      strict_slash=True),
          RedirectRoute('/auth/facebook-login/', 
                      handler='web.authenticate.FacebookHandler', 
                      name = 'facebook-login',
                      strict_slash=True),
          RedirectRoute('/auth/confirm-email/', 
                      handler='web.authenticate.ConfirmEmailHandler', 
                      name = 'confirm-email',
                      strict_slash=True),
          RedirectRoute('/auth/reset-password/', 
                      handler='web.authenticate.ResetPasswordHandler', 
                      name = 'reset-password',
                      strict_slash=True),
          RedirectRoute('/auth/reset-password-complete/', 
                      handler='web.authenticate.ResetPasswordCompleteHandler', 
                      name = 'reset-password-complete',
                      strict_slash=True)]

config = {
          'webapp2_extras.sessions': {
                                      'secret_key': '28206929354928434436111284652724869173016715794802'
                                      } 
          }

#ndb.toplevel to make sure all async requests finish before handler exits
app = ndb.toplevel(webapp2.WSGIApplication(routes, config=config))